﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using StoreWinform.ServiceReference1;
using StoreWinform.Model;
using BusinessCommon.Request;
using BusinessCommon.Responce;
using BusinessCommon.DTO;
using BusinessCommon.Utilities;
using System.Threading.Tasks;
using StoreWinform.View;

namespace StoreWinform.Controller
{
    /// <summary>
    /// 
    /// </summary>
    public class ChangePassFormController
    {
        LoginServiceClient client = new LoginServiceClient("firstService");
        private static ChangePassFormController instance = new ChangePassFormController();
        private LoginFormModel model;
        private ChangePassForm ChangePassView;

        public ChangePassFormController()
        {
            this.ChangePassView = GetView;
            this.model = GetModelView;
        }

        public ChangePassForm GetView
        {
            get
            {
                if (this.ChangePassView == null)
                {
                    this.ChangePassView = new ChangePassForm();
                }
                return this.ChangePassView;
            }
        }

        public LoginFormModel GetModelView
        {
            get
            {
                if (this.model == null)
                {
                    this.model = new LoginFormModel();
                }
                return this.model;
            }
        }

        public static ChangePassFormController Instance
        {
            get { return ChangePassFormController.instance; }
        }

        public void OpenChangePassForm()
        {
            ChangePassView.SetController(this);
            ChangePassView.Visible = true;
        }

        public void ClearInstance()
        {
            model = null;
            instance = null;
            ChangePassView = null;
        }

        /// <summary>
        /// User login
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool UpdateData(string password, string oldPassword, string userId)
        {
            UserRequest request = new UserRequest();
            request.Password = oldPassword;
            request.UserName = userId;
            request.PasswordChange = StringUtilities.EncryptPassword(password);

            // Service: LoginAction
            UserResponce responce = client.UpdatePwdAction(request);

            if (responce.IsSuccess)
            {
                return true;
            }
            else
            {
                if (responce.ErrorCode != null)
                {
                    if (ChangePassView.InvokeRequired)
                    {
                        ChangePassView.BeginInvoke(new Action(() => ChangePassView.ShowMessage(responce.ErrorCode, SmEnum.MSG_BOX_TYPE.ERR)));
                    }
                }
            }
            return false;
        }
    }
}
